package org.example.word;

import com.aspose.pdf.Document;
import com.aspose.pdf.SaveFormat;
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfNewDocument;
import com.spire.pdf.graphics.PdfFont;
import com.spire.pdf.graphics.PdfFontFamily;
import com.spire.pdf.graphics.PdfFontStyle;
import com.spire.pdf.graphics.fonts.PdfUsedFont;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.concurrent.CountDownLatch;

/**
 * @author yanglizdf
 * @description pdf转word文件
 */

public class Pdf2WordTask implements Runnable {

    private String docPath;
    private File file;
    private CountDownLatch countDownLatch;


    public void setDocPath(String docPath) {
        this.docPath = docPath;
    }

    public void setCountDownLatch(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    public void setFile(File file) {
        this.file = file;
    }

    @Override
    public void run() {
        spirePdf();
    }

    public void asposePdf() {
        long old = System.currentTimeMillis();
        try {
            String pdfPath=file.getAbsolutePath();
            //新建一个word文档
            String wordPath = docPath + file.getName().substring(0, file.getName().length() - 4) + ".docx";
            FileOutputStream  os = new FileOutputStream(wordPath);
            //doc是将要被转化的word文档
            Document doc = new Document(new FileInputStream(file));
            //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            doc.save(os, SaveFormat.DocX);
            os.close();
            //转化用时
            long now = System.currentTimeMillis();
            System.out.println("Pdf 转 Word 共耗时：" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            System.out.println("Pdf 转 Word 失败...");
            e.printStackTrace();
        }finally {

            countDownLatch.countDown();
        }
    }

    private void spirePdf(){
        PdfDocument pdfDocument = new PdfDocument();
        try {
            System.out.println("[数据平台][pdf2word][file:"+file.getName()+"][size:"+file.length()+"][path:"+file.getAbsolutePath()+"]");
            pdfDocument.loadFromFile(file.getAbsolutePath());
            //用 UsedFonts 属性获取文档中使用的所有字体

            //获取文档中的所有字体
            String filePath = docPath + file.getName().substring(0, file.getName().length() - 4) + ".docx";
            pdfDocument.saveToFile(filePath, FileFormat.DOCX);
            System.out.println("[数据平台][pdf2word][file:"+file.getName()+"][完成]");
        }catch (Exception e) {
            System.out.println("[数据平台][pdf2word][file:"+file.getName()+"][size:"+file.length()+"][path:"+file.getAbsolutePath()+"],error:"+e);
        }finally {
            pdfDocument.close();
            countDownLatch.countDown();
        }
    }

    private void spirePdf2(){
        PdfDocument pdfDocument = new PdfDocument();
        try {
            System.out.println("[数据平台][pdf2word][file:"+file.getName()+"][size:"+file.length()+"][path:"+file.getAbsolutePath()+"]");
            pdfDocument.loadFromFile(file.getAbsolutePath());
            //获取文档中的所有字体
            String filePath = docPath + file.getName().substring(0, file.getName().length() - 4) + ".docx";
            pdfDocument.saveToFile(filePath, FileFormat.DOCX);
            System.out.println("[数据平台][pdf2word][file:"+file.getName()+"][完成]");
        }catch (Exception e) {
            System.out.println("[数据平台][pdf2word][file:"+file.getName()+"][size:"+file.length()+"][path:"+file.getAbsolutePath()+"],error:"+e);
        }finally {
            pdfDocument.close();
            countDownLatch.countDown();
        }
    }





}
